﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Xml;


namespace Javavaj.iHunter.UI
{
    public partial class GlobalSettingsForm : DevComponents.DotNetBar.Office2007Form
    {
        private bool initializing = true;
        public GlobalSettingsForm()
        {
            InitializeComponent();
        }
        private XmlDocument settings;
        public XmlDocument Settings
        {
            get
            {
                settings.DocumentElement["AutoUpdate"].InnerText = checkAutoUpdate.Checked.ToString();
                settings.DocumentElement["AutoUpdateInterval"].InnerText = TimeSpan.FromMinutes(this.textUpdateInterval.Value).ToString();
                settings.DocumentElement["AutoDownloadFile"].InnerText = checkAutoDownload.Checked.ToString();
                settings.DocumentElement["AutoDownloadSizeLimit"].InnerText = ((checkLimitSize.Checked) ? ((textSizeLimit.Value * 1024).ToString()) : ((-1).ToString()));
                settings.DocumentElement["Language"].InnerText = ((checkUseDefaultLanguage.Checked) ? (string.Empty) : (comboDisplayLanguage.SelectedItem.ToString()));


                return settings;

            }
            set
            {
                initializing = true;
                checkAutoUpdate.Checked = bool.Parse(value.DocumentElement["AutoUpdate"].InnerText);
                textUpdateInterval.Value = (int)(TimeSpan.Parse(value.DocumentElement["AutoUpdateInterval"].InnerText).TotalMinutes);
                checkAutoDownload.Checked = bool.Parse(value.DocumentElement["AutoDownloadFile"].InnerText);
                long sizeLimit = long.Parse(value.DocumentElement["AutoDownloadSizeLimit"].InnerText);
                if (sizeLimit > 0)
                {
                    checkLimitSize.Checked = true;
                    textSizeLimit.Value = (int)(sizeLimit / 1024);
                }
                else
                {
                    checkLimitSize.Checked = false;
                    textSizeLimit.Value = 1024;
                }
                object selItem = null;
                string lang = value.DocumentElement["Language"].InnerText;
                if (lang.Length != 0)
                {
                   foreach (DevComponents.Editors.ComboItem item in comboDisplayLanguage.Items)
                    {

                        if (item.Text == lang)
                        {
                            selItem = item;

                            break;
                        }
                    }
                }
                if (selItem == null)
                {
                    checkUseDefaultLanguage.Checked = true;
                    comboDisplayLanguage.SelectedIndex = 0;
                }
                else
                {
                    checkUseDefaultLanguage.Checked = false;
                    comboDisplayLanguage.SelectedItem = selItem;

                }
                initializing = false;
                settings = value;
            }
        }
        private void UpdateButtonStatus()
        {
            buttonGeneral.Checked = tabItemGeneral.IsSelected;
            buttonChannels.Checked = tabItemChannels.IsSelected; ;
            buttonResources.Checked = tabItemResources.IsSelected; ;
        }

        private void buttonGeneral_Click(object sender, EventArgs e)
        {

            tabSettingPanels.SelectedTab = tabItemGeneral;
            UpdateButtonStatus();
        }


        private void buttonChannels_Click(object sender, EventArgs e)
        {
            tabSettingPanels.SelectedTab = tabItemChannels;
           
            UpdateButtonStatus();

        }

  

        private void buttonResources_Click(object sender, EventArgs e)
        {
            tabSettingPanels.SelectedTab = tabItemResources;
            UpdateButtonStatus();


        }

        private void GlobalSettingsForm_Load(object sender, EventArgs e)
        {
            this.Font = SystemFonts.MenuFont;
        }



        private void checkUseDefaultLanguage_CheckedChanged(object sender, EventArgs e)
        {
            this.comboDisplayLanguage.Enabled = !checkUseDefaultLanguage.Checked;
            this.labelDisplayLanuguage.Enabled = !checkUseDefaultLanguage.Checked;
            if (!initializing)
                this.labelWarningRestart.Visible = true;
        }

        private void checkAutoUpdate_CheckedChanged(object sender, EventArgs e)
        {
            this.textUpdateInterval.Enabled = checkAutoUpdate.Checked;
            labelUpdateInterval.Enabled = checkAutoUpdate.Checked;
            labelX1.Enabled = checkAutoUpdate.Checked;
        }

        private void checkAutoDownload_CheckedChanged(object sender, EventArgs e)
        {
            this.checkLimitSize.Enabled = checkAutoDownload.Checked;
        }

        private void checkLimitSize_CheckedChanged(object sender, EventArgs e)
        {
            textSizeLimit.Enabled = checkLimitSize.Enabled && checkLimitSize.Checked;
            labelX3.Enabled = checkLimitSize.Enabled && checkLimitSize.Checked;
            labelX4.Enabled = checkLimitSize.Enabled && checkLimitSize.Checked;
        }

        private void checkLimitSize_EnabledChanged(object sender, EventArgs e)
        {
            textSizeLimit.Enabled = checkLimitSize.Enabled && checkLimitSize.Checked;

        }

        private void comboDisplayLanguage_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!initializing)
                this.labelWarningRestart.Visible = true;

        }

        private void Display_Warning(object sender, EventArgs e)
        {
            this.labelWarningRestart.Visible = true;

        }

        private void buttonSendEmail_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("mailto:高翔 <gaox05@mails.tsinghua.edu.cn>?subject=iHunter建议与缺陷报告&body=在这里输入正文!");
        }

        private void buttonHomePage_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://ihunter.net9.org/");
        }



        
        
    }
}
